<?php

namespace backend\services\config;

use backend\extensions\ApiBaseService;
use backend\models\config\AdminMenu;

/**
 * Desc 菜单服务类
 * @author HUI
 */
class AdminMenuService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params, $select = [])
    {
        //检索条件
        $condition = ['AND'];
        if ($params['name']) {
            $condition[] = ['LIKE', 'name', $params['name']];
        }
        if (isset($params['id']) && !empty($params['id'])) {
            $condition[] = ['in', 'id', $params['id']];
        }
        $params['page'] = 1;
        $params['page_size'] = 10000;
        $list = AdminMenu::getDataAll($condition, $params['page'], $params['page_size'], 'sort ASC', $select);
        $list['list'] = AdminMenu::arrayTree($list['list']);
        return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function create($params)
    {
        if (AdminMenu::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 更新
     * @param array $params 参数 
     * **/
    public static function update($params)
    {
        $condition = ['id' => $params['id']];
        unset($params['id']);
        if (AdminMenu::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * **/
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        AdminMenu::del(['parent' => $params['id']]);
        if (AdminMenu::del($condition) == false) {

            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * **/
    public static function detail($params)
    {
        $condition = ['id' => $params['id']];
        $data = AdminMenu::getDataOne($condition);
        return self::jsonSuccess($data);
    }
}
